package com.orhon.smartcampus.modules.baseinfo.fetcher;


import com.orhon.smartcampus.modules.baseinfo.dao.SchoolcalendarDayDao;
import com.orhon.smartcampus.modules.baseinfo.entity.SchoolcalendarDay;
import com.orhon.smartcampus.modules.baseinfo.entity.SchoolcalendarWeek;
import com.orhon.smartcampus.modules.baseinfo.entity.Semester;
import com.orhon.smartcampus.modules.baseinfo.gqlModel.GQLSchoolCalendarDay;
import com.orhon.smartcampus.modules.baseinfo.gqlModel.GQLSchoolCalendarWeek;
import com.orhon.smartcampus.modules.baseinfo.gqlModel.GQLSemester;
import com.orhon.smartcampus.modules.baseinfo.mm.SchoolCalendarDayMapper;
import com.orhon.smartcampus.modules.baseinfo.mm.SchoolCalendarWeekMapper;
import com.orhon.smartcampus.modules.baseinfo.mm.SemesterMapper;

import graphql.schema.DataFetcher;
import graphql.schema.DataFetchingEnvironment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class SchoolCalendarDayFetcher implements DataFetcher {


	@Autowired
	SchoolcalendarDayDao schoolcalendarDayDao;

	@Override
	public Object get(DataFetchingEnvironment env) throws Exception {

		int id = Integer.parseInt(env.getArgument("id").toString());

		String hql = "from SchoolcalendarDay where deleted_at = null and id = "+id;
		SchoolcalendarDay item =  schoolcalendarDayDao.findId(hql, null);
		if(item!=null) {
			GQLSchoolCalendarWeek gqlSchoolCalendarWeek= null;
			if (env.getSelectionSet().contains("week")){
				SchoolcalendarWeek week = item.getSchoolcalendarWeek();
				gqlSchoolCalendarWeek = SchoolCalendarWeekMapper.MAPPER.toSchoolcalendarWeek(week);
			}
			
			GQLSemester gqlSemester = null;
			if (env.getSelectionSet().contains("semester")){
				Semester semester = item.getSemester();
				gqlSemester = SemesterMapper.MAPPER.toSemester(semester);
			}

		    GQLSchoolCalendarDay schoolCalendarDay = SchoolCalendarDayMapper.MAPPER.toSchoolCalendarDay(item);
		    schoolCalendarDay.setSemester(gqlSemester);
		    schoolCalendarDay.setWeek(gqlSchoolCalendarWeek);
			return schoolCalendarDay;
		}
		return null;
	}


}
